<?php require_once 'common.php';
	$conn = getConnection();
	
	$type = isset($_GET['type']) ? $_GET['type'] : '';
	$siteCheck = isset($_GET['s']) ? $_GET['s'] : '';
	switch ($siteCheck){
		case 's' :
			$siteCookiesCur = isset($_COOKIE[COOKIE_SITE_ADD]) ? $_COOKIE[COOKIE_SITE_ADD] : '' ;
			$siteCookiesTmp = isset($_COOKIE[COOKIE_SITE_ADD_TMP]) ? $_COOKIE[COOKIE_SITE_ADD_TMP] : '' ;
			setcookie(COOKIE_SITE_ADD, $siteCookiesTmp);
			$_COOKIE[COOKIE_SITE_ADD] = $siteCookiesTmp;
			break;
			
		case 'c' :
			$siteCookiesCur = isset($_COOKIE[COOKIE_SITE_ADD]) ? $_COOKIE[COOKIE_SITE_ADD] : '' ;
			setcookie(COOKIE_SITE_ADD_TMP, $siteCookiesCur);
			break;
			
		default :
			break;
	}
	
	$getBySites = isset($_COOKIE[COOKIE_SITE_ADD]) ? ' WHERE i.site_id IN (' . $_COOKIE[COOKIE_SITE_ADD] . ')' : ' WHERE 1=1';
	$search = '';
	$size = 20;
	$page = 1;
	$limit = " LIMIT 0, {$size}";
	
	if (isset($_GET['p'])) {
		$page = $_GET['p'];
		$offset = (int)$page * $size;
		$limit = " LIMIT {$offset},{$size}";
	}
	
	$sql = 'SELECT i.`id`,i.`link`,i.`title`,i.`lead`,i.`detail`,i.`images`,i.`fb_like`,i.`tw_like`,s.`name`,s.color FROM items i INNER JOIN sites s ON i.site_id=s.id' . $getBySites;
	$orderBy = ' ORDER BY i.`date` DESC';
	
	if (isset($_GET['q'])) {
		$search = $_GET['q'];
		$sql .= " AND (i.`detail` LIKE '%{$search}%' OR i.`title` LIKE '%{$search}%')";
	}
	
	switch ($type){
		case 'fav' :
			$getByFav = isset($_COOKIE[COOKIE_FAV_NAME]) ? ' AND i.id IN (' . $_COOKIE[COOKIE_FAV_NAME] . ')' : '';
			if($getByFav == ''){
				$result = array();			
			} else {
				$result = fetchData($sql . $getByFav . $orderBy);
			}
			break;
		
		case 'change_fav' :
			
			$itemId = $_POST['item_id'];
			$itemCookies = isset($_COOKIE[COOKIE_FAV_NAME]) ? $_COOKIE[COOKIE_FAV_NAME] : '';
			$itemCookiesArr = explode(',', $itemCookies);
			if(in_array($itemId, $itemCookiesArr)){
				$itemCookiesArr = array_diff($itemCookiesArr, array($itemId));
			} else {
				$itemCookiesArr[] = $itemId;
			}
			setcookie(COOKIE_FAV_NAME, trim(implode(',', $itemCookiesArr), ','));
			
			$reQuery = $sql;
			if($_POST['current_action'] == 'fav'){
				if(empty($itemCookiesArr)){
					echo json_encode(array('data' => ''));
					exit;
				}
				$getByFav = ' WHERE i.id IN (' . implode(',', $itemCookiesArr) . ')';
				$reQuery .= $getByFav;
			}

			$dataReturned = renderBox(fetchData($reQuery . $orderBy), $itemCookiesArr, true);
			echo json_encode(array('data' => $dataReturned));
			exit;
		
		case 'change_site' :
			$siteCookies = isset($_COOKIE[COOKIE_SITE_ADD_TMP]) ? $_COOKIE[COOKIE_SITE_ADD_TMP] : '' ;
			$siteCookies = explode(',', $siteCookies);
			$siteId = $_POST['site_id'];
			if(in_array($siteId, $siteCookies)){
				$siteCookies = array_diff($siteCookies, array($siteId));
			} else {
				$siteCookies[] = $siteId;
			}
			setcookie(COOKIE_SITE_ADD_TMP, trim(implode(',', $siteCookies), ','));
			echo json_encode(array('status' => 'success'));
			exit;
			break;
			
		default :
			$result = fetchData($sql . $orderBy . $limit);
			break;
	}
	
	closeConnection($conn);
?>

<!DOCTYPE HTML PUBLIC>
<html>
    <head>
        <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
        <link rel="stylesheet" href="css/style.css" type="text/css" />
        <link rel="stylesheet" href="css/colorbox.css" type="text/css" />
        <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
        <title>Nurse News</title>
        <script></script>
    </head>
    <body>
        <div class="main_wrap">
            <div class="header">
                <div class="logo">
                	<img src="images/logo.png" alt="Logo"/>
                </div>
				<form method="get" class="searchbox">
					<input type="text" size="40" placeholder="" id="searchtext" class="inputtext" name="q" value="<?php echo $search ?>" />
					<button type="submit" class="search-button">&nbsp;</button>
				</form>
           </div>
            <div class="body">
            		<div class="menu">
                    <ul>
                        <li class="menu1"><a href="<?php echo DOMAIN_NAME . 'index.php'?>" class="top_tab"></a></li>
                        <li class="menu2"><a href="<?php echo DOMAIN_NAME . 'index.php?type=fav'?>" class="fav_tab"></a></li>
                        <li class="menu3"><a href="javascript:void(0)" id="show_select_site" href="#select_site_block" class="channel_tab"></a></li>
                    </ul>
                </div>
                <span style="clear: both; display:block"></span>
                <div style='display:none'>
	                <div class="smax_left container" id="select_site_block" style="margin-top:100px;width:80%">
	                	<?php echo renderSites();?>
	                </div>
                </div>
                <div class="smax_left" id="news_box">
                    <?php echo renderBox($result); ?>
					
					<div class="clearfix"></div>
					<ul class="pager">
					<?php if ($page>1): ?>
						<li><a href="?p=<?php echo $page-1 ?>">Previous</a></li>
					<?php endif ?>
					<?php if (count($result)==$size): ?>
						<li><a href="?p=<?php echo $page+1 ?>">Next</a></li>
					<?php endif ?>
					</ul>
				</div>
            </div>
        </div>
        <input type="hidden" id="current_action" value="<?php echo $type ;?>">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
		<script src="js/jquery.colorbox-min.js"></script>
		<script src="js/script.js"></script>
    </body>
</html>